<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: laserConnect.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.4</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('laserConnect_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">laserConnect.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>example showing how to connect to laser rangefinders attached to the robot and retrieve data from them. What kinds of laser or lasers are connected to the robot, and their parameters, are loaded from the robot's parameter file in the params directory. First the default for this type of mobile robot is loaded (e.g. p3dx-sh.p for a Pioneer 3 DX), followed by a parameter file specific to this individual robot (based on its 'name'), if such a parameter file exists. Laser parameters may also be given as command line options, use the -help option to list them. This program will only connect to any lasers that have LaserAutoConnect true in the parameter file, or if the -connectLaser or -cl command-line options are given. You may need to edit the parameter file for your robot to set LaserAutoConnect to true.</p>
<p>TODO: show how to force laser connect even if autoconnect is false.</p>
<p>This program will work either with the MobileSim simulator or on a real robot's onboard computer.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">  <a name="a0"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line">  <a name="_a1"></a><a class="code" href="classArRobot.html">ArRobot</a> robot;</div><div class="line">  <a name="_a2"></a><a class="code" href="classArArgumentParser.html">ArArgumentParser</a> parser(&amp;argc, argv);</div><div class="line">  parser.<a name="a3"></a><a class="code" href="classArArgumentParser.html#a501694b62a7f8b0c3cadd84aeafe1b35">loadDefaultArguments</a>();</div><div class="line"></div><div class="line">  <a name="_a4"></a><a class="code" href="classArRobotConnector.html">ArRobotConnector</a> robotConnector(&amp;parser, &amp;robot);</div><div class="line">  <a name="_a5"></a><a class="code" href="classArLaserConnector.html">ArLaserConnector</a> laserConnector(&amp;parser, &amp;robot, &amp;robotConnector);</div><div class="line"></div><div class="line">  <span class="comment">// Connect to the robot, get some initial data from it such as type and name,</span></div><div class="line">  <span class="comment">// and then load parameter files for this robot.</span></div><div class="line">  <span class="keywordflow">if</span>(!robotConnector.<a name="a6"></a><a class="code" href="classArRobotConnector.html#ab192ac393d9cc1e5b61b1a82b08d085d">connectRobot</a>())</div><div class="line">  {</div><div class="line">    <a name="a7"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a8"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;lasersExample: Could not connect to the robot.&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span>(parser.<a name="a9"></a><a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">    {</div><div class="line">        <span class="comment">// -help not given</span></div><div class="line">        <a name="a10"></a><a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">        <a name="a11"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!<a name="a12"></a><a class="code" href="classAria.html#a1b090c01d88bb420b8cf8e0384d25ee1">Aria::parseArgs</a>())</div><div class="line">  {</div><div class="line">    <a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(2);</div><div class="line">    <span class="keywordflow">return</span> 2;</div><div class="line">  }</div><div class="line">  </div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a13"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;lasersExample: Connected to robot.&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Start the robot processing cycle running in the background.</span></div><div class="line">  <span class="comment">// True parameter means that if the connection is lost, then the </span></div><div class="line">  <span class="comment">// run loop ends.</span></div><div class="line">  robot.<a name="a14"></a><a class="code" href="classArRobot.html#a3c3d878cfd11a2ef91230005a83bfcaa">runAsync</a>(<span class="keyword">true</span>);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// Connect to laser(s) as defined in parameter files.</span></div><div class="line">  <span class="comment">// (Some flags are available as arguments to connectLasers() to control error behavior and to control which lasers are put in the list of lasers stored by ArRobot. See docs for details.)</span></div><div class="line">  <span class="keywordflow">if</span>(!laserConnector.<a name="a15"></a><a class="code" href="classArLaserConnector.html#ab97ce6a5fedebf44526773bc674b970e">connectLasers</a>())</div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;Could not connect to configured lasers. Exiting.&quot;</span>);</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(3);</div><div class="line">    <span class="keywordflow">return</span> 3;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Allow some time to read laser data</span></div><div class="line">  <a name="a16"></a><a class="code" href="group__easy.html#ga4dfb878a3f9123b8dd96ae05b2fd94cb">ArUtil::sleep</a>(500);</div><div class="line"></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;Connected to all lasers.&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Print out some data from each connected laser.</span></div><div class="line">  <span class="keywordflow">while</span>(robot.<a name="a17"></a><a class="code" href="group__easy.html#ga98b5d608fb9f33aec47a3fe1995dc5b6">isConnected</a>())</div><div class="line">  {</div><div class="line">    <span class="keywordtype">int</span> numLasers = 0;</div><div class="line"></div><div class="line">      <span class="comment">// Get a pointer to ArRobot&#39;s list of connected lasers. We will lock the robot while using it to prevent changes by tasks in the robot&#39;s background task thread or any other threads. Each laser has an index. You can also store the laser&#39;s index or name (laser-&gt;getName()) and use that to get a reference (pointer) to the laser object using ArRobot::findLaser().</span></div><div class="line">      robot.<a name="a18"></a><a class="code" href="classArRobot.html#a5aecc7ac998c9a31b82fdb904efa8609">lock</a>();</div><div class="line">      std::map&lt;int, ArLaser*&gt; *lasers = robot.<a name="a19"></a><a class="code" href="classArRobot.html#a62ad0a5f2cc0973f08f8d85bea1d5d7b">getLaserMap</a>();</div><div class="line"></div><div class="line">      <span class="keywordflow">for</span>(std::map&lt;int, ArLaser*&gt;::const_iterator i = lasers-&gt;begin(); i != lasers-&gt;end(); ++i)</div><div class="line">      {</div><div class="line">        <span class="keywordtype">int</span> laserIndex = (*i).first;</div><div class="line">        <a name="_a20"></a><a class="code" href="classArLaser.html">ArLaser</a>* laser = (*i).second;</div><div class="line">        <span class="keywordflow">if</span>(!laser)</div><div class="line">            <span class="keywordflow">continue</span>;</div><div class="line">        ++numLasers;</div><div class="line">        laser-&gt;<a name="a21"></a>lockDevice();</div><div class="line"></div><div class="line">        <span class="comment">// The current readings are a set of obstacle readings (with X,Y positions as well as other attributes) that are the most recent set from teh laser.</span></div><div class="line">        std::list&lt;ArPoseWithTime*&gt; *currentReadings = laser-&gt;<a name="a22"></a><a class="code" href="classArRangeDevice.html#a072ac84980687ed353b3e5c2e36d6d68">getCurrentBuffer</a>(); <span class="comment">// see ArRangeDevice interface doc</span></div><div class="line"></div><div class="line">        <span class="comment">// There is a utility to find the closest reading wthin a range of degrees around the laser, here we use this laser&#39;s full field of view (start to end)</span></div><div class="line">        <span class="comment">// If there are no valid closest readings within the given range, dist will be greater than laser-&gt;getMaxRange().</span></div><div class="line">        <span class="keywordtype">double</span> angle = 0;</div><div class="line">        <span class="keywordtype">double</span> dist = laser-&gt;<a name="a23"></a><a class="code" href="classArRangeDevice.html#a27b080d64bdedd8f593ffcc6c373d609">currentReadingPolar</a>(laser-&gt;<a name="a24"></a><a class="code" href="classArLaser.html#ac589039384c421703756eac2861899bf">getStartDegrees</a>(), laser-&gt;<a name="a25"></a><a class="code" href="classArLaser.html#aa6aa3c7373dc933b8f32bb36c23cd088">getEndDegrees</a>(), &amp;angle);</div><div class="line"></div><div class="line">        <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;Laser #%d (%s): %s. Have %d &#39;current&#39; readings. Closest reading is at %3.0f degrees and is %2.4f meters away.&quot;</span>, laserIndex, laser-&gt;<a name="a26"></a><a class="code" href="classArRangeDevice.html#adf76b644d209c28628a1f7ba4047b60b">getName</a>(), (laser-&gt;<a name="a27"></a><a class="code" href="classArLaser.html#aae5c36df5b59ad861debb895e255479b">isConnected</a>() ? <span class="stringliteral">&quot;connected&quot;</span> : <span class="stringliteral">&quot;NOT CONNECTED&quot;</span>), currentReadings-&gt;size(), angle, dist/1000.0);</div><div class="line">                laser-&gt;<a name="a28"></a>unlockDevice();</div><div class="line">        }</div><div class="line">    <span class="keywordflow">if</span>(numLasers == 0)</div><div class="line">        <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;No lasers.&quot;</span>);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line">        <span class="comment">// Unlock robot and sleep for 5 seconds before next loop.</span></div><div class="line">    robot.<a name="a29"></a><a class="code" href="classArRobot.html#a403d9de010dc2b8d478cd0b14b2d9491">unlock</a>();</div><div class="line">    <a class="code" href="group__easy.html#ga4dfb878a3f9123b8dd96ae05b2fd94cb">ArUtil::sleep</a>(5000);</div><div class="line">   }</div><div class="line"></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;lasersExample: exiting.&quot;</span>);</div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
